#!/bin/bash
# 
# Faust project
# version management tool
#

function warn {
	echo "WARNING ! This tools is intended to change the current version number"
	echo "NOT to recall a previous Faust version !"
}
function major {
	echo $1 | sed 's/\..*//'
}
function minor {
	echo $1 | sed 's/[0-9]*\.\([0-9]*\)\..*/\1/'
}
function patch {
	echo $1 | sed 's/[0-9]*\.[0-9]*\.\([0-9]*\)/\1/'
}
function winVer {
	echo $1 | sed "s/\./,$2/g"
}
function rcUpdate {
 	sed "s/FILEVERSION[ 	].*/FILEVERSION $2/" $1 | \
 	sed "s/PRODUCTVERSION[ 	].*/PRODUCTVERSION $2/" | \
 	sed "s/VALUE \"FileVersion\"..*/VALUE \"FileVersion\", \"$3\"/" | \
 	sed "s/VALUE \"ProductVersion\"..*/VALUE \"ProductVersion\", \"$3\"/"
}

if [ $# -ne 1 ]
then
	echo "Faust version management utility"
	echo "usage: version version_num"
	echo "       version_num format: n.n.n"
	warn
	exit 1
fi

warn
echo "Type OK to continue... "
read CONT
[ "$CONT" == "OK" ] || exit 1


ROOT=.
VERSION=$1
MAJOR=$(major $VERSION)
MINOR=$(minor $VERSION)
PATCH=$(patch $VERSION)
WINVERS=$(winVer $VERSION)",0"
WINVERS2=$(winVer $VERSION " ")", 0"

if [ -d compiler ]
then
	echo "moving version number to $VERSION"
else
	echo "this script must be called from the Faust project root directory"
	exit 1
fi

LIBFAUSTACTION=$ROOT/.github/workflows/libfaust.yml
echo " updating $LIBFAUSTACTION"
sed "s/FAUST_VERSION*: *..*/FAUST_VERSION: $VERSION/" $LIBFAUSTACTION > TMP$$
mv -f TMP$$ $LIBFAUSTACTION

MAKEF=$ROOT/Makefile
echo " updating $MAKEF"
sed "s/^version *:= *..*/version := $VERSION/" $MAKEF > TMP$$
mv -f TMP$$ $MAKEF

MAKEF=$ROOT/build/Makefile
echo " updating $MAKEF"
sed "s/^VERSION *:= *..*/VERSION := $VERSION/" $MAKEF > TMP$$
mv -f TMP$$ $MAKEF

CMAKE=$ROOT/build/CMakeLists.txt
echo " updating $CMAKE"
sed "s/set (VERSION[ 	]*[0-9].*)/set (VERSION $VERSION)/" $CMAKE > TMP$$
mv TMP$$ $CMAKE

MAKERELEASE=$ROOT/build/MakeRelease.bat
echo " updating $MAKERELEASE"
sed "s/^SET VERSION=[0-9].*/SET VERSION=$VERSION/" $MAKERELEASE > TMP$$
mv TMP$$ $MAKERELEASE

FAUST=$ROOT/architecture/faust/export.h
echo " updating $FAUST"
sed "s/FAUSTVERSION[ 	]\".*\"/FAUSTVERSION \"$VERSION\"/" $FAUST > TMP$$
mv -f TMP$$ $FAUST
sed "s/FAUSTMAJORVERSION[ 	].*/FAUSTMAJORVERSION $MAJOR/" $FAUST > TMP$$
mv -f TMP$$ $FAUST
sed "s/FAUSTMINORVERSION[ 	].*/FAUSTMINORVERSION $MINOR/" $FAUST > TMP$$
mv -f TMP$$ $FAUST
sed "s/FAUSTPATCHVERSION[ 	].*/FAUSTPATCHVERSION $PATCH/" $FAUST > TMP$$
mv -f TMP$$ $FAUST

FAUSTRC=$ROOT/windows/faust.rc
echo " updating $FAUSTRC"
rcUpdate $FAUSTRC $WINVERS "$WINVERS2" > TMP$$
mv -f TMP$$ $FAUSTRC

FAUSTDLLRC=$ROOT/windows/faustdll.rc
echo " updating $FAUSTDLLRC"
rcUpdate $FAUSTRC $WINVERS "$WINVERS2" > TMP$$
mv -f TMP$$ $FAUSTDLLRC

MAN=$ROOT/documentation/man/man-header.txt
echo " updating $MAN"
sed "s/% man(1) Version [ 	]*[0-9].*/% man(1) Version $VERSION (`LC_ALL=C date +%d-%B-%Y`) | Faust man page/" $MAN > TMP$$
mv -f TMP$$ $MAN

COPYING=$ROOT/COPYING.txt
echo " updating $COPYING"
sed "s/FAUST compiler, Version[ 	]*[0-9].*/FAUST compiler, Version $VERSION/" $COPYING > TMP$$
mv -f TMP$$ $COPYING

DOXYFILE1=$ROOT/documentation/libfaust/Doxyfile
echo " updating $DOXYFILE1"
sed "s/PROJECT_NUMBER         = [ 	]*[0-9].*/PROJECT_NUMBER         = $VERSION/" $DOXYFILE1 > TMP$$
mv -f TMP$$ $DOXYFILE1

DOXYFILE2=$ROOT/compiler/Doxyfile
echo " updating $DOXYFILE2"
sed "s/PROJECT_NUMBER         = [ 	]*[0-9].*/PROJECT_NUMBER         = $VERSION/" $DOXYFILE2 > TMP$$
mv -f TMP$$ $DOXYFILE2

DOXYFILE3=$ROOT/documentation/libfaustremote/Doxyfile
echo " updating $DOXYFILE3"
sed "s/PROJECT_NUMBER         = [ 	]*[0-9].*/PROJECT_NUMBER         = $VERSION/" $DOXYFILE3 > TMP$$
mv -f TMP$$ $DOXYFILE3

DOXYFILE4=$ROOT/documentation/compiler/Doxyfile
echo " updating $DOXYFILE4"
sed "s/PROJECT_NUMBER         = [ 	]*[0-9].*/PROJECT_NUMBER         = $VERSION/" $DOXYFILE4 > TMP$$
mv -f TMP$$ $DOXYFILE4

make man

echo $VERSION > version.txt
echo "### You need to recompile Faust for the change to take effect."
exit 0
